package com.sport;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.sport.pojo.Account;
import com.sport.pojo.AllCourses;
import com.sport.util.MySQLitOpenHelper;
import com.sport.util.ToastUtil;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private EditText et_name, et_pwd;
    private Button btn_login, btn_go_register;
    private String name, pwd;
    MySQLitOpenHelper Mydb;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this::onClick);
        btn_go_register = findViewById(R.id.btn_go_register);
        btn_go_register.setOnClickListener(this::onClick);
        Mydb = new MySQLitOpenHelper(this);
    }

    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                name = et_name.getText().toString().trim();
                pwd = et_pwd.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.toastShort(this, "用户名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.toastShort(this, "密码不能为空");
                    return;
                }
                Account check_account = new Account(name, pwd);
                List<Account> accounts = Mydb.selectAccountByName(check_account);
                if (accounts.isEmpty()) {
                    ToastUtil.toastShort(this, "用户不存在");
                    return;
                } else {
                    for (Account a : accounts) {
                        if (pwd.equals(a.getPwd())) {
                            ToastUtil.toastShort(this, "登录成功");
                            List<AllCourses> courses = Mydb.showAllCourses();
                            if (courses.isEmpty()) {
                                Mydb.initCoursesData();
                            } else {
                                System.out.println(courses);
                                System.out.println("-----------课程信息都在这了----------");
                            }
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            intent.putExtra("name", name);
                            startActivity(intent);
                            finish(); // 结束当前 Activity
                            return;
                        }
                    }
                    ToastUtil.toastShort(this, "密码错误");
                }
                break;

            case R.id.btn_go_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;

        }
    }


}